use serde::{Deserialize, Serialize};
use crate::config::get;

/// 返回客户端的Token信息
///
/// 前端一般使用store进行存储后提交时需要在header中携带
#[derive(Debug, Deserialize, Serialize)]
pub struct Token {
    name: Option<String>,
    timeout: Option<i64>,
    code: Option<String>,
    login_name: Option<String>,
    token: Option<String>,
}

impl Token {
    pub fn new(login_name: &str, token_str: &str) -> Self {
        let timeout = get().token().unwrap().timeout();
        let name = get().token().unwrap().name();
        // 从redis中获取对应的用的值
        let code = "3123123";
        Self {
            name: Some(name.to_string()),
            timeout: Some(timeout),
            code: Some(code.to_string()),
            login_name: Some(login_name.to_string()),
            token: Some(token_str.to_string()),
        }
    }

    pub fn to_string(&self) -> String {
        serde_json::to_string(self).unwrap_or_else(|_| String::from("转换错误!"))
    }
}